package com.zycfc.zsf.boot.util.collection;

import com.zycfc.zsf.boot.util.bean.*;
import java.lang.reflect.*;
import java.util.*;
import org.slf4j.*;

public class MapUtils
{
    private static final Logger log;
    
    public static boolean isEmpty(final Map<?, ?> map) {
        return map == null || map.isEmpty();
    }
    
    public static boolean isNotEmpty(final Map<?, ?> map) {
        return !isEmpty(map);
    }
    
    public static Map<String, Object> objectToMap(final Object obj) {
        final Map<String, Object> map = new HashMap<String, Object>();
        final List<Field> fields = BeanUtils.getAllFields(obj.getClass());
        for (final Field field : fields) {
            field.setAccessible(true);
            try {
                map.put(field.getName(), field.get(obj));
            }
            catch (IllegalArgumentException | IllegalAccessException ex2) {
                final Exception ex;
                final Exception e = ex;
                MapUtils.log.error("\u8f6c\u6362\u5f02\u5e38", (Throwable)e);
            }
        }
        return map;
    }
    
    public static void removeNullEntry(final Map map) {
        removeNullKey(map);
        removeNullValue(map);
    }
    
    public static void removeNullKey(final Map map) {
        final Set set = map.keySet();
        final Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            final Object obj = iterator.next();
            remove(obj, iterator);
        }
    }
    
    public static void removeNullValue(final Map map) {
        final Set set = map.keySet();
        final Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            final Object obj = iterator.next();
            final Object value = map.get(obj);
            remove(value, iterator);
        }
    }
    
    private static void remove(final Object obj, final Iterator iterator) {
        if (obj instanceof String) {
            final String str = (String)obj;
            if (isEmpty(str)) {
                iterator.remove();
            }
        }
        else if (obj instanceof Collection) {
            final Collection col = (Collection)obj;
            if (col == null || col.isEmpty()) {
                iterator.remove();
            }
        }
        else if (obj instanceof Map) {
            final Map temp = (Map)obj;
            if (temp == null || temp.isEmpty()) {
                iterator.remove();
            }
        }
        else if (obj instanceof Object[]) {
            final Object[] array = (Object[])obj;
            if (array == null || array.length <= 0) {
                iterator.remove();
            }
        }
        else if (obj == null) {
            iterator.remove();
        }
    }
    
    private static boolean isEmpty(final Object obj) {
        return obj == null || obj.toString().length() == 0;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)MapUtils.class);
    }
}
